home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / GANTTCH.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  9.8 KB  |  292 lines

  1. {**********************************************}
  2. {   TGanttSeries (derived from TPointSeries)   }
  3. {   Copyright (c) 1996-98 by David Berneda     }
  4. {**********************************************}
  5. {$I teedefs.inc}
  6. unit GanttCh;
  7.  
  8. interface
  9.  
  10. { This unit shows how a new Chart Series component can be easily created.
  11.   TGanttSeries derives from standard TPointSeries.
  12.   Each point in the series is drawn like a Gantt horizontal bar.
  13.   Each point has a Start and End values that are used to draw the Gantt bar
  14.   with its corresponding screen length in the horizontal plane. }
  15. Uses Classes,Graphics,Chart,Series,Teengine,WinTypes,TeCanvas;
  16.  
  17. Type
  18.   TGanttSeries=class(TPointSeries)
  19.   private
  20.     { The Gantt Start values are implicit stored in XValues }
  21.     FEndValues     : TChartValueList; { <-- Gantt bar's end values storage }
  22.     FNextTask      : TChartValueList;  { <-- Used to connect lines }
  23.     FConnectingPen : TChartPen;
  24.     Procedure SetConnectingPen(Value:TChartPen);
  25.     Procedure SetEndValues(Value:TChartValueList);
  26.     Procedure SetStartValues(Value:TChartValueList);
  27.     Procedure SetNextTask(Value:TChartValueList);
  28.     Function GetStartValues:TChartValueList;
  29.   protected
  30.     procedure DrawValue(ValueIndex:Longint); override; { <-- main draw method }
  31.     Procedure DrawMark(ValueIndex:Longint; Const St:String; APosition:TSeriesMarkPosition); override;
  32.     Function ClickedPointer( ValueIndex,tmpX,tmpY:Longint;
  33.                              x,y:Longint):Boolean; override;
  34.   public
  35.     Constructor Create(AOwner: TComponent); override;
  36.     Destructor Destroy; override;
  37.     Function AddGantt(Const AStart,AEnd,AY:Double; Const AXLabel:String{$IFDEF D4}=''{$ENDIF}):Longint;
  38.     Function AddGanttColor( Const AStart,AEnd,AY:Double;
  39.                             Const AXLabel:String{$IFDEF D4}=''{$ENDIF};
  40.                             AColor:TColor{$IFDEF D4}=clTeeColor{$ENDIF} ):Longint;
  41.     Procedure Assign(Source:TPersistent); override;
  42.     Procedure CalcHorizMargins(Var LeftMargin,RightMargin:Integer); override;
  43.     Procedure ClearTempValue(ValueList:TChartValueList); override;
  44.     Procedure FillSampleValues(NumValues:Longint); override; { <-- to add random end values }
  45.     Function GetEditorClass:String; override;
  46.     Function IsValidSourceOf(Value:TChartSeries):Boolean; override;
  47.     Function MandatoryValueList:TChartValueList; override;
  48.     Function MaxXValue:Double; override;  { <-- adds end values }
  49.     Procedure PrepareForGallery(IsEnabled:Boolean); override;
  50.   published
  51.     property ConnectingPen:TChartPen read FConnectingPen write SetConnectingPen;
  52.     property StartValues:TChartValueList read GetStartValues write SetStartValues;
  53.     property EndValues:TChartValueList read FEndValues write SetEndValues;
  54.     property NextTask:TChartValueList read FNextTask write SetNextTask;
  55.   end;
  56.  
  57. implementation
  58.  
  59. Uses SysUtils,TeeProcs,TeeConst;
  60.  
  61. { TGanttSeries }
  62. Constructor TGanttSeries.Create(AOwner: TComponent);
  63. Begin
  64.   inherited Create(AOwner);
  65.   ClickableLine:=False; { only allow click on Pointer (Gantt Bar) }
  66.   CalcVisiblePoints:=False; { draw all points }
  67.   XValues.Name:=TeeMsg_ValuesGanttStart;
  68.   XValues.DateTime:=True;
  69.   ColorEachPoint:=True;
  70.   FEndValues :=TChartValueList.Create(Self,TeeMsg_ValuesGanttEnd);
  71.   FEndValues.DateTime:=True;
  72.   FNextTask  :=TChartValueList.Create(Self,TeeMsg_ValuesGanttNextTask);
  73.   Pointer.Style:=psRectangle; { <-- a Horizontal Gantt Bar (by default) }
  74.   FConnectingPen:=CreateChartPen;
  75. end;
  76.  
  77. Destructor TGanttSeries.Destroy;
  78. Begin
  79.   FConnectingPen.Free;
  80.   inherited Destroy;
  81. End;
  82.  
  83. Procedure TGanttSeries.SetEndValues(Value:TChartValueList);
  84. Begin
  85.   SetChartValueList(FEndValues,Value); { standard method }
  86. end;
  87.  
  88. Procedure TGanttSeries.SetNextTask(Value:TChartValueList);
  89. Begin
  90.   SetChartValueList(FNextTask,Value); { standard method }
  91. end;
  92.  
  93. Procedure TGanttSeries.SetConnectingPen(Value:TChartPen);
  94. Begin
  95.   FConnectingPen.Assign(Value);
  96. end;
  97.  
  98. { Helper method, special to Gantt bar series }
  99. Function TGanttSeries.AddGanttColor( Const AStart,AEnd,AY:Double;
  100.                                      Const AXLabel:String;
  101.                                      AColor:TColor ):Longint;
  102. begin
  103.   result:=AddXY(AStart,AY,AXLabel,AColor); { standard add X,Y }
  104.   FEndValues.TempValue:=AEnd;
  105.   FNextTask.TempValue:=-1;
  106.   AddValue(result);
  107. end;
  108.  
  109. { Helper method, special to Gantt bar series }
  110. Function TGanttSeries.AddGantt( Const AStart,AEnd,AY:Double;
  111.                                 Const AXLabel:String):Longint;
  112. Begin
  113.   result:=AddGanttColor(AStart,AEnd,AY,AXLabel {$IFNDEF D4},clTeeColor{$ENDIF});
  114. end;
  115.  
  116. Procedure TGanttSeries.FillSampleValues(NumValues:Longint);
  117.  
  118.   Function GanttSampleStr(Index:Longint):String;
  119.   begin
  120.     Case Index of
  121.       0: result:=TeeMsg_GanttSample1;
  122.       1: result:=TeeMsg_GanttSample2;
  123.       2: result:=TeeMsg_GanttSample3;
  124.       3: result:=TeeMsg_GanttSample4;
  125.       4: result:=TeeMsg_GanttSample5;
  126.       5: result:=TeeMsg_GanttSample6;
  127.       6: result:=TeeMsg_GanttSample7;
  128.       7: result:=TeeMsg_GanttSample8;
  129.       8: result:=TeeMsg_GanttSample9;
  130.      else
  131.         result:=TeeMsg_GanttSample10;
  132.      end;
  133.   end;
  134.  
  135. Const NumGanttSamples=10;
  136. Var Added        : Longint;
  137.     t            : Longint;
  138.     tmpY         : Longint;
  139.     tt           : Longint;
  140.     tmpStartTask : TDateTime;
  141.     tmpEndTask   : TDateTime;
  142. Begin
  143.   Clear;
  144.   { some sample values to see something at design mode }
  145.   for t:=0 to MinLong( NumValues, NumGanttSamples+Random(20) ) do
  146.   begin
  147.     tmpStartTask:=Date+t*3+Random(5);
  148.     tmpEndTask:=tmpStartTask+9+Random(16);
  149.     tmpY:=(t mod 10);
  150.     Added:=AddGantt( tmpStartTask, { Start }
  151.                      tmpEndTask,   { End }
  152.                      tmpY,         { Y value }
  153.                      GanttSampleStr(tmpY) { some sample label text }
  154.                      );
  155.     { Connect Gantt points: }
  156.     for tt:=0 to added-1 do
  157.     if (NextTask[tt]=-1) and (tmpStartTask>EndValues[tt]) then
  158.     begin
  159.       NextTask[tt]:=added;
  160.       break;
  161.     end;
  162.   end;
  163.   RefreshSeries;
  164. end;
  165.  
  166. Function TGanttSeries.ClickedPointer( ValueIndex,tmpX,tmpY:Longint;
  167.                                       x,y:Longint):Boolean;
  168. begin
  169.   result:=(x>=tmpX) and (x<=CalcXPosValue(EndValues[ValueIndex])) and
  170.           (Abs(tmpY-Y)<Pointer.VertSize);
  171. end;
  172.  
  173. procedure TGanttSeries.DrawValue(ValueIndex:Longint);
  174. var x1              : Longint;
  175.     x2              : Longint;
  176.     Y               : Longint;
  177.     tmpHalfHorizSize: Longint;
  178.     HalfWay         : Longint;
  179.     tmpNextTask     : Longint;
  180.     xNext           : Longint;
  181.     YNext           : Longint;
  182.     tmpStyle        : TSeriesPointerStyle;
  183. Begin
  184. { This overrided method is the main paint for Gantt bar points. }
  185.   if Pointer.Visible then
  186.   With ParentChart.Canvas do
  187.   Begin
  188.     Pointer.PrepareCanvas(ValueColor[ValueIndex]);
  189.     X1:=CalcXPos(ValueIndex);
  190.     X2:=CalcXPosValue(EndValues[ValueIndex]);
  191.     tmpHalfHorizSize:=(x2-x1) div 2;
  192.     Y:=CalcYPos(ValueIndex);
  193.     {$IFNDEF D1}
  194.     if Assigned(OnGetPointerStyle) then
  195.        tmpStyle:=OnGetPointerStyle(Self,ValueIndex)
  196.     else
  197.     {$ENDIF}
  198.        tmpStyle:=Pointer.Style;
  199.     Pointer.DrawPointer( ParentChart.View3D,
  200.                          x1+tmpHalfHorizSize,
  201.                          Y,
  202.                          tmpHalfHorizSize,
  203.                          Pointer.VertSize,
  204.                          ValueColor[ValueIndex],tmpStyle);
  205.     if FConnectingPen.Visible then
  206.     Begin
  207.       tmpNextTask:=Round(NextTask[ValueIndex]);
  208.       if (tmpNextTask>=0) and (tmpNextTask<Count) then
  209.       Begin
  210.         Pen.Assign(FConnectingPen);
  211.         Brush.Style:=bsClear;
  212.         XNext:=CalcXPos(tmpNextTask);
  213.         HalfWay:=X2+((XNext-X2) div 2);
  214.         YNext:=CalcYPos(tmpNextTask);
  215.         LineWithZ(X2,Y,HalfWay,Y,MiddleZ);
  216.         LineTo3D(HalfWay,YNext,MiddleZ);
  217.         LineTo3D(XNext,YNext,MiddleZ);
  218.       End;
  219.     end;
  220.   end;
  221. end;
  222.  
  223. Function TGanttSeries.MaxXValue:Double;
  224. Begin
  225.   result:=MaxDouble(inherited MaxXValue,FEndValues.MaxValue);
  226. end;
  227.  
  228. Procedure TGanttSeries.DrawMark( ValueIndex:Longint; Const St:String;
  229.                                  APosition:TSeriesMarkPosition);
  230. Begin
  231.   With APosition do
  232.   begin
  233.     Inc(LeftTop.X,(CalcXPosValue(EndValues[ValueIndex])-ArrowFrom.X) div 2);
  234.     Inc(LeftTop.Y,Height div 2);
  235.   end;
  236.   inherited DrawMark(ValueIndex,St,APosition);
  237. End;
  238.  
  239. Procedure TGanttSeries.ClearTempValue(ValueList:TChartValueList);
  240. Begin
  241.   if ValueList=FNextTask then FNextTask.TempValue:=-1
  242.                          else inherited ClearTempValue(ValueList);
  243. End;
  244.  
  245. Function TGanttSeries.MandatoryValueList:TChartValueList;
  246. Begin
  247.   Result:=StartValues;  { <-- this is used in DataSource Expert Dialog }
  248. End;
  249.  
  250. Function TGanttSeries.GetStartValues:TChartValueList;
  251. Begin
  252.   result:=XValues;
  253. end;
  254.  
  255. Procedure TGanttSeries.SetStartValues(Value:TChartValueList);
  256. Begin
  257.   SetXValues(Value);
  258. end;
  259.  
  260. Procedure TGanttSeries.PrepareForGallery(IsEnabled:Boolean);
  261. begin
  262.   inherited PrepareForGallery(IsEnabled);
  263.   ColorEachPoint:=IsEnabled;
  264.   Pointer.VertSize:=3;
  265. end;
  266.  
  267. Function TGanttSeries.GetEditorClass:String;
  268. begin
  269.   result:='TGanttSeriesEditor';  { <-- dont translate ! }
  270. end;
  271.  
  272. Procedure TGanttSeries.Assign(Source:TPersistent);
  273. begin
  274.   if Source is TGanttSeries then
  275.      FConnectingPen.Assign(TGanttSeries(Source).FConnectingPen);
  276.   inherited Assign(Source);
  277. end;
  278.  
  279. Function TGanttSeries.IsValidSourceOf(Value:TChartSeries):Boolean;
  280. begin
  281.   result:=Value is TGanttSeries; { Only Gantt can be assigned to Gantt }
  282. end;
  283.  
  284. Procedure TGanttSeries.CalcHorizMargins(Var LeftMargin,RightMargin:Integer);
  285. begin
  286.   inherited CalcHorizMargins(LeftMargin,RightMargin); { Fix Delphi 1 "Top Axis" }
  287. end;
  288.  
  289. initialization
  290.  RegisterTeeSeries(TGanttSeries,TeeMsg_GalleryGantt,TeeMsg_GalleryStandard,1);
  291. end.
  292.